package com.ruoyi.tianliangedu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tianliangedu.domain.TianliangCourse;
import com.ruoyi.tianliangedu.domain.TianliangSubject;
import com.ruoyi.tianliangedu.service.ITianliangCourseService;
import com.ruoyi.tianliangedu.service.ITianliangSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理Controller
 *
 * @author lsj
 * @date 2021-07-12
 */
@Controller
@RequestMapping("/tianliangedu/course")
public class TianliangCourseController extends BaseController {
    private String prefix = "tianliangedu/course";

    @Autowired
    private ITianliangCourseService tianliangCourseService;

    @Autowired
    private ITianliangSubjectService tianliangSubjectService;

    @RequiresPermissions("tianliangedu:course:view")
    @GetMapping()
    public String course(ModelMap modelMap) {
        TianliangSubject tianliangSubject = new TianliangSubject();
        List<TianliangSubject> list = tianliangSubjectService.selectTianliangSubjectList(tianliangSubject);
        // 当专题状态是停用的时候,新增课程中移除该专题
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSubState() == 2) {
                list.remove(i);
            }
        }
        modelMap.put("subName", list);
        return prefix + "/course";
    }

    /**
     * 查询课程管理列表
     */
    @RequiresPermissions("tianliangedu:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TianliangCourse tianliangCourse) {
        startPage();
        List<TianliangCourse> list = tianliangCourseService.selectTianliangCourseList(tianliangCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @RequiresPermissions("tianliangedu:course:export")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TianliangCourse tianliangCourse) {
        List<TianliangCourse> list = tianliangCourseService.selectTianliangCourseList(tianliangCourse);
        ExcelUtil<TianliangCourse> util = new ExcelUtil<TianliangCourse>(TianliangCourse.class);
        return util.exportExcel(list, "课程管理数据");
    }

    /**
     * 新增课程管理
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        TianliangSubject tianliangSubject = new TianliangSubject();
        List<TianliangSubject> list = tianliangSubjectService.selectTianliangSubjectList(tianliangSubject);
        // 当专题状态是停用的时候,新增课程中移除该专题
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSubState() == 2) {
                list.remove(i);
            }
        }
        modelMap.put("subName", list);
        return prefix + "/add";
    }

    /**
     * 新增保存课程管理
     */
    @RequiresPermissions("tianliangedu:course:add")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TianliangCourse tianliangCourse) {
        return toAjax(tianliangCourseService.insertTianliangCourse(tianliangCourse));
    }

    /**
     * 修改课程管理
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        TianliangCourse tianliangCourse = tianliangCourseService.selectTianliangCourseById(courseId);
        mmap.put("tianliangCourse", tianliangCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程管理
     */
    @RequiresPermissions("tianliangedu:course:edit")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TianliangCourse tianliangCourse) {
        return toAjax(tianliangCourseService.updateTianliangCourse(tianliangCourse));
    }

    /**
     * 删除课程管理
     */
    @RequiresPermissions("tianliangedu:course:remove")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tianliangCourseService.deleteTianliangCourseByIds(ids));
    }
}
